import java.util.Scanner;

public class Main {
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        long[] arr = new long[n];
        long sum = 0l;
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextLong();
            sum += arr[i];
        }

        long ret = Integer.MAX_VALUE;
        //A队从哪个位置开始枚举
        for (int i = 0; i < n; i++) {
            long sumA = 0;
            //开始枚举A，并计算这段区域的总和
            for (int l = i; l < n; l++) {
                sumA += arr[l];
                //B对从哪个位置开始枚举
                for (int j = l + 1; j < n; j++) {
                    int sumB = 0;
                    //开始枚举B，并计算这段区域的总和
                    for (int k = j; k < n; k++) {
                        sumB += arr[k];
                        ret = Math.min(ret, Math.abs(sumA - sumB));
                    }
                }
            }
        }

        System.out.println(ret);

        scanner.close();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        long[] arr = new long[n];
        long sum = 0l;
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextLong();
            sum += arr[i];
        }

        long[] dp = new long[n + 1];
        for (int i = 1; i < n + 1; i++) {
            dp[i] = dp[i - 1] + arr[i - 1];
        }

        long ret = Integer.MAX_VALUE;
        //A从哪个位置开始枚举
        for (int i = 1; i < n + 1; i++) {
            long sumA = 0;
            //开始枚举A
            for (int j = i; j < n + 1; j++) {
                sumA = dp[j] - dp[i - 1];
                //B从哪个位置开始枚举
                for (int k = j + 1; k < n + 1; k++) {
                    long sumB = 0;
                    //开始枚举B
                    for (int l = k; l < n + 1; l++) {
                        sumB = dp[l] - dp[k - 1];
                        ret = Math.min(ret, Math.abs(sumA - sumB));
                    }
                }
            }
        }
        System.out.println(ret);

        scanner.close();
    }
}
